import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';

import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/widgets/appbar/common_app_bar.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/input/common_text_field.dart';
import 'package:flutter_09_game/widgets/widget_profile_item.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'profile_find_login_password_logic.dart';

class ProfileFindLoginPasswordPage extends StatelessWidget {
  ProfileFindLoginPasswordPage({super.key});

  final ProfileFindLoginPasswordLogic logic = Get.put(ProfileFindLoginPasswordLogic());

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<ProfileFindLoginPasswordLogic>(
      appBar: CommonAppBar(
        title: LocaleKeys.retrieveLoginPassword.tr,
        actions: [
          GestureDetector(
            onTap: () {
              AppUserManager().showServiceSheet();
            },
            child: AutoImage(
              Assets.commonServiceBtn,
              width: 32.w,
            ),
          ),
          10.horizontalSpace,
        ],
      ),
      builder: (_) {
        return Container(
          padding: EdgeInsets.all(12.w),
          margin: EdgeInsets.all(16.w)
              .copyWith(bottom: 16 + ScreenUtil().bottomBarHeight),
          decoration: BoxDecoration(
              color: AppColor.navBgColor,
              borderRadius: BorderRadius.circular(16.w)),
          child: Column(
            children: [
              Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [

                        WidgetProfileItem(
                          LocaleKeys.newLoginPassword.tr,
                          _buildInput(logic.newPasswordController,
                              LocaleKeys.pleaseEnterNewPassword.tr),
                        ),
                        16.verticalSpaceFromWidth,
                        WidgetProfileItem(
                          LocaleKeys.confirmNewLoginPassword.tr,
                          _buildInput(logic.newConfirmPasswordController,
                              LocaleKeys.pleaseEnterNewPassword.tr),
                        ),
                        16.verticalSpaceFromWidth,
                        Obx(() {
                          return WidgetProfileItem(
                              LocaleKeys.securityQuestion.tr,
                              Container(
                                padding: EdgeInsets.symmetric(
                                    vertical: 14.w, horizontal: 12.w),
                                decoration: BoxDecoration(
                                    color: AppColor.color2D2F35,
                                    borderRadius: BorderRadius.circular(8.w)),
                                child: Row(
                                  children: [
                                    Text(
                                      logic.securityQuestion.value.question
                                          ?.Content ??
                                          '',
                                      style: TextStyle(
                                          color:
                                          AppColor.textPrimary.withOpacity(0.3),
                                          fontSize: 14.sp,
                                          fontWeight: FontWeight.w500),
                                    )
                                  ],
                                ),
                              ));
                        }),
                        16.verticalSpaceFromWidth,

                        Obx(() {
                          return WidgetProfileItem(
                              (logic.securityQuestion.value.notice ?? '').isNotEmpty
                                  ? '${LocaleKeys.securityAnswerHint.tr}:'
                                  : LocaleKeys.enterSecurityAnswer.tr,
                              leftStr: logic.securityQuestion.value.notice,
                              CommonTextField(
                                textEditingController: logic.answerController,
                                hintText: LocaleKeys.enterSecurityAnswer.tr,
                                backgroundColor: Colors.black.withOpacity(0.3),
                                borderColor: AppColor.textPrimary.withOpacity(0.15),
                                maxLength: 100,
                                onChanged: (tex) {
                                  logic.validateForm();
                                },
                              ));
                        }),

                        // securityQuestion
                      ],
                    ),
                  )),
              12.verticalSpaceFromWidth,
              Obx(() => CommonButton.expend(
                title: LocaleKeys.ok.tr,
                enable: logic.isFormValid.value,
                onClick: () {
                  logic.postUserFindPassword();
                },
              )),
            ],
          ),
        );
      },
    );
  }


  Widget _buildInput(TextEditingController controller, String hintStr) {
    return CommonTextField(
      textEditingController: controller,
      isPassword: true,
      backgroundColor: Colors.black.withOpacity(0.3),
      keyboardType: TextInputType.visiblePassword,
      inputFormatters: [
        // 允许输入特殊字符，不做字符过滤
        LengthLimitingTextInputFormatter(10),
        // 限制最大长度10位
      ],
      hintText: hintStr,
      maxLength: 10,
      leftWidget: AutoImage(
        Assets.loginPsw,
        width: 24.w,
      ),
    );
  }
}
